﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace WinFileTransfer
{
    class WinConnection
    {
        public string path { get; set; }
        public string user { get; set; }
        public string pass { get; set; }

        public string LinkFile()
        {
            if (path != "" && user != "" && pass != "")
            {
                StringBuilder sb = new StringBuilder();
                try
                {
                    using (System.Diagnostics.Process process = new System.Diagnostics.Process())
                    {
                        process.StartInfo = new System.Diagnostics.ProcessStartInfo()
                        {
                            FileName = "cmd.exe",
                            UseShellExecute = false,
                            RedirectStandardInput = false,
                            RedirectStandardOutput = true,
                            RedirectStandardError = true,
                            CreateNoWindow = true
                        };
                        process.StartInfo.Arguments = @"/c net use \\" + path + " " + pass + " /user:" + user;//要输入到命令程序的字符，其中"/c"表示执行完命令后马上退出
                        //process.StartInfo.Arguments = "/c net view \\" + path;
                        string output = null;
                        if (process.Start())
                        {
                            output = output ?? process.StandardOutput.ReadToEnd();
                            process.WaitForExit();
                            output = output ?? process.StandardOutput.ReadToEnd();//读取进程的输出
                            LogHelper.Log.Info(output);
                        }
                    }
                }
                catch (Exception e)
                {
                    LogHelper.Log.Error(sb, e);
                    return e.ToString();
                }
                return path;
            }
            else
            {
                return "路径、用户名或密码错误";
            }
        }

        public string LinkFileAsyn()
        {
            if (path != "" && user != "" && pass != "")
            {
                try
                {
                    using (System.Diagnostics.Process process = new System.Diagnostics.Process())
                    {
                        process.StartInfo.FileName = "cmd.exe";
                        process.StartInfo.Arguments = "/c net view \\" + path;
                        process.StartInfo.UseShellExecute = false;
                        process.StartInfo.RedirectStandardInput = true;
                        process.StartInfo.RedirectStandardOutput = true;
                        process.StartInfo.RedirectStandardError = true;
                        process.StartInfo.CreateNoWindow = true;
                        process.OutputDataReceived += (sender, e) =>
                        {
                            LogHelper.Log.Info(e.Data);
                        };
                        process.Start();
                        process.BeginOutputReadLine();
                        process.WaitForExit();
                        Thread.Sleep(2000);
                    }
                }
                catch (Exception e)
                {
                    LogHelper.Log.Error(e);
                    return e.ToString();
                }
                return path;
            }
            else
            {
                return "路径、用户名或密码错误";
            }
        }

        public void KillLink()
        {
            System.Diagnostics.Process process = new System.Diagnostics.Process();//创建进程对象
            System.Diagnostics.ProcessStartInfo startinfo = new System.Diagnostics.ProcessStartInfo();//创建进程时使用的一组值，如下面的属性
            startinfo.FileName = "cmd.exe";//设定需要执行的命令程序
            //以下是隐藏cmd窗口的方法
            startinfo.Arguments = "/c " + @"Net Use /delete * /yes";//设定参数，要输入到命令程序的字符，其中"/c"表示执行完命令后马上退出
            startinfo.UseShellExecute = false;      //不使用系统外壳程序启动
            startinfo.RedirectStandardInput = false;//不重定向输入
            startinfo.RedirectStandardOutput = true;//重定向输出，而不是默认的显示在dos控制台上
            startinfo.CreateNoWindow = true;        //不创建窗口
            process.StartInfo = startinfo;
            string output = "";
            try
            {
                if (process.Start())        //开始进程
                {
                    process.WaitForExit();
                    output = process.StandardOutput.ReadToEnd();//读取进程的输出
                }
            }
            catch (Exception e)
            {
                LogHelper.Log.Error(e);
            }
            finally
            {
                if (process != null)
                {
                    process.Close();
                }
            }
        }
    }
}
